package com.bw.lib_common.utils;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.LENGTH_SHORT;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;

/**
 * @author ykj
 * @date 2025-10-15 18:05
 * @description
 */
public class ToastUtils {
    private static Toast mToast;
    private static void cancel(){
        //新的Toast进来的时候 取消上次的Toast
        if (mToast != null){
            mToast.cancel();
            mToast = null;
        }
    }
    private static void showToast(Context mContext, String text, int duration){
        cancel();
        int height = mContext.getResources().getDisplayMetrics().heightPixels;
        mToast = Toast.makeText(mContext,text,duration);
        mToast.show();
    }

    private static void showToast(Context mContext, int resId, int duration){
        cancel();
        int height = mContext.getResources().getDisplayMetrics().heightPixels;
        mToast.setGravity(Gravity.TOP,0,height / 6);
        mToast.show();
    }
    public  static void showLong(final Context activity,final String message) {
        if (message.isEmpty()) return;
        if (activity != null && !TextUtils.isEmpty(message))
            showToast(activity.getApplicationContext(),message,LENGTH_SHORT);
    }
    public static void showSHort(final Context activity, final int msgRes){
        showToast(activity,msgRes,LENGTH_SHORT);
    }
    public static void showLong(final Context activity,final int msgRes) {
        showToast(activity,msgRes,LENGTH_LONG);
    }
}
